package com.example.fw;

import org.openqa.selenium.By;
import com.example.tests.ContactObject;


public class ContactHelper extends HelperWithWebDriverBase {

	public ContactHelper (ApplicationManager manager) {
		super (manager);
	}
	
	public void createContact(ContactObject contactObject) {
		manager.getNavigationHelper().openMainPage();
		initContactCreation();
		fillContactForm(contactObject);
		submitContactCreation();
		manager.getNavigationHelper().doToHomePage();
	}

	public void modifyContact(int i, ContactObject contactObject) {
		manager.getNavigationHelper().openMainPage();
		initContactModification(i);
		modifyContactForm(contactObject);
		submitContactModification();
		manager.getNavigationHelper().doToHomePage();
	}
	
	public void deleteContact(int i) {
		manager.getNavigationHelper().openMainPage();
		checkContact(i);
		initContactModification(i);
		click(By.xpath("/html/body/div/div[4]/form/input[11]"));
		manager.getNavigationHelper().doToHomePage();
	}
	
	private void initContactCreation() {
		click(By.linkText("add new"));
	}
	
	private void initContactModification(int i) {
		checkContact(i);
		click(By.cssSelector("img[alt=\"Edit\"]"));
	}
	
	private void checkContact(int i) {
		click(By.name("selected[]"));
	}
	
	private void fillContactForm(ContactObject contactObject) {
		type(By.name("firstname"), contactObject.firstname);
		type(By.name("lastname"), contactObject.lastname);
		type(By.name("address"), contactObject.address);
		type(By.name("home"), contactObject.homephone);
		type(By.name("mobile"), contactObject.mobilephone);
		type(By.name("work"), contactObject.workphone);
		type(By.name("email"), contactObject.email);
		type(By.name("email2"), contactObject.email2);
		select(By.name("bday"), contactObject.bday);
		select(By.name("bmonth"), contactObject.bmonth);
		type(By.name("byear"), contactObject.byear);
		select(By.name("new_group"), contactObject.group);
		type(By.name("address2"), contactObject.address2);
		type(By.name("phone2"), contactObject.homephone2);
	}

	private void modifyContactForm(ContactObject contactObject) {
		type(By.name("firstname"), contactObject.firstname);
		type(By.name("lastname"), contactObject.lastname);
		type(By.name("address"), contactObject.address);
		type(By.name("home"), contactObject.homephone);
		type(By.name("mobile"), contactObject.mobilephone);
		type(By.name("work"), contactObject.workphone);
		type(By.name("email"), contactObject.email);
		type(By.name("email2"), contactObject.email2);
		select(By.name("bday"), contactObject.bday);
		select(By.name("bmonth"), contactObject.bmonth);
		type(By.name("byear"), contactObject.byear);
		type(By.name("address2"), contactObject.address2);
		type(By.name("phone2"), contactObject.homephone2);
	}
	
	private void submitContactCreation() {
		click(By.name("submit"));
	}

	private void submitContactModification() {
		click(By.name("update"));
	}

}			
	